 import org.apache.commons.codec.digest.DigestUtils as DU 

class BootStrap {
	def md5pass = DU.md5Hex("pass")
	
     def init = { servletContext ->
		initSecurityData()
		initDevelopmentLoadData()
		
     }
     def destroy = {
     }
	 
	 def initDevelopmentLoadData(){
		def prj1 = new Project(name:"ProjectTest 1",description:"test 1").save();
		def prj2 = new Project(name:"ProjectTest 2",description:"test 2").save();
		
		def trk1 = new Tracker(name:"Tracker 1",description:"tracker 1",project:prj1).save();
		def trk2 = new Tracker(name:"Tracker 2",description:"tracker 2",project:prj2).save();
		
		def user1 = new User(username:"user1", userRealName:"User1", passwd:md5pass, 
							enabled:true, email:"user1@example.com", email_show:true, 
							description:"User").save();
							
		def artf1 = new Artifact(reporter:user1, description:"artifact 1", 
									name:"name artifact 1", tracker:trk1).save();					
		def artf2 = new Artifact(reporter:user1, description:"artifact 2", 
									name:"name artifact 2", tracker:trk1).save();
		def artf3 = new Artifact(reporter:user1, description:"artifact 3", 
									name:"name artifact 3", tracker:trk2).save();
		def artf4 = new Artifact(reporter:user1, description:"artifact 4", 
									name:"name artifact 4", tracker:trk2).save();
									
		def comment1 = new Comment(creator:user1, artifact:artf1, body:"body comment1", creationDate: new Date()).save();
		def comment2 = new Comment(creator:user1, artifact:artf1, body:"body comment2", creationDate: new Date()).save();
		def comment3 = new Comment(creator:user1, artifact:artf1, body:"body comment3", creationDate: new Date()).save();
		def comment4 = new Comment(creator:user1, artifact:artf1, body:"body comment4", creationDate: new Date()).save();
	 }
	 
	 def initSecurityData() {
		
		// use it to create an encoded password
		//def md5pass = DU.md5Hex("pass") //authenticateService.passwordEncoder("pass")

		// Create two sample users
		// NB: you must specify all the Person attributes, otherwise
		//     Grails will fail quietly to add these to the database
		def user_root = new User(username:"root",
		   userRealName:"Root User",
		   passwd:md5pass,
		   enabled:true,
		   email:"root@example.com",
		   email_show:true,
		   description:"Desc").save()
		def user_admin = new User(username:"admin",
		   userRealName:"Admin User",
		   passwd:md5pass,
		   enabled:true,
		   email:"admin@example.com",
		   email_show:false,
		   description:"Desc").save()

		// Add some sample Roles, add the users to those roles
		def role_superuser = new Role(description:"Superuser",
		   authority:"ROLE_SUPERUSER")
		role_superuser.addToPeople(user_root)
		role_superuser.save()

		// See AcegiConfig.groovy, at the bottom, there's a defaultrole
		// setting, you must make one to allow registration to work.
		def role_user = new Role(description:"User Role",
		   authority:"ROLE_USER")
		role_user.addToPeople(user_admin)
		role_user.addToPeople(user_root)
		role_user.save()

		// Create some Request Maps
		new Requestmap(url:"/project/create/**",
		  configAttribute:"ROLE_SUPERUSER").save()
		new Requestmap(url:"/artifact/**",
		  configAttribute:"ROLE_USER").save()
			 
	 }
} 